<template>
  <a-card :bordered="false" :form="form">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <!--<a-form-item label="名称">
              <a-input placeholder="请输入商品名称" v-model="queryParam.goodsName"></a-input>
            </a-form-item>-->
            <!--<a-form-item label="商品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['goodsName']"
                :trigger-change="true"
                org-fields="id,name"
                dest-fields="goodsId,goodsName"
                code="goods_select"
                :field="getPopupField('goodsId')"
                @callback="popupCallback"/>
            </a-form-item>-->
            <a-form-item label="商品名称">
              <j-popup placeholder="请选择商品名称"
                       code="goods_select"
                       org-fields="name,id"
                       dest-fields="goodsName,goodsId"
                       field="goodsName"
                       :trigger-change="true"
                       @callback="popupCallback"
              />
              {{queryParam.goodsName}}
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="审核状态">
              <j-dict-select-tag placeholder="请选择审核状态" v-model="queryParam.applyStatus" dictCode="shelf_apply_status"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="上下架状态">
              <j-dict-select-tag placeholder="请选择上下架状态" v-model="queryParam.status" dictCode="goods_status"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="标题">
              <a-input placeholder="请输入标题" v-model="queryParam.title"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!--<a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>-->
            </span>
          </a-col>
        </a-row>

      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!--<a-button type="primary" icon="download" @click="handleExportXls('商品上架')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <!--审核状态-->
        <template slot="applyStatus_dictText" slot-scope="text,record">
          <div v-html="text" v-if="record.applyStatus==0" style="color: #ff8000;"></div>
          <div v-html="text" v-else-if="record.applyStatus==1" style="color: #8000ff;"></div>
          <div v-html="text" v-else-if="record.applyStatus==2" style="color: #409eff;"></div>
          <div v-html="text" v-else style="color: #ff0000;"></div>
        </template>
        <!--上下架状态-->
        <template slot="status" slot-scope="text, record,index">
          <a-popconfirm :disabled="record.applyStatus==2&&record.status!==0&&record.status!==-1||record.applyStatus!==2&&record.status!==0&&record.status!==-1?false:true" :title="record.status == 0?'确定上架吗?':'确定下架吗?'" @confirm="() => handleGoodsUpDown(record)">
            <a-switch checked-children="已上架"  class="ssss" un-checked-children="已下架" disabled :checked="record.status == '0' || record.status == '-1'?false:true" />
          </a-popconfirm>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
<!--          //againShelf-->
          <span v-if="(record.status==0 && record.applyStatus==3 && record.againShelf !== 1) || (record.status==0 &&record.applyStatus==0&&record.againShelf !== 1)">
            <a @click="handleEditCustom(record)">编辑</a>
            <a-divider type="vertical" />
          </span>
          <!--  修改数量 3个满足条件：不能是秒杀 且 已下架 且 审核通过 -->
          <span v-if="record.againShelfFlag==1">
            <a @click="handleFixNum(record)">修改数量</a>
            <a-divider type="vertical" />
          </span>
          <!--审核状态[0:未送审 1:已送审 2:审核通过 3:审核拒绝]-->
          <span v-show="record.applyStatus==0" v-has="'goodsshelf:songshen'">
            <a-popconfirm title="确定送审吗?" @confirm="() => handleSend(record.id)">
              <a>送审</a>
            </a-popconfirm>
            <a-divider type="vertical" />
          </span>

          <!--审核按钮   =====2 通过 3拒绝-->
          <span v-show="record.applyStatus==1" v-has="'goodsshelf:shenhe'">
            <a @click="shenheid(record.id)">审核</a>
            <!--<a-popover title="审核" trigger="click">
              <template slot="content">
                <a-button style="margin-right: 10px" type="danger" @click="shenhe(record.id,3)">拒 绝</a-button>
                <a-button  type="primary" @click="shenhe(record.id,2)">通 过</a-button>
              </template>
              <a>审核</a>
            </a-popover>-->
            <a-divider type="vertical" />
          </span>
          <!--<span >
            <a @click="handleGoodsUpDown(record)">{{record.status=='0'?'上架':'下架'}}</a>
            <a-divider type="vertical" />
          </span>-->
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <!--<a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>-->
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <goods-shelf-modal ref="modalForm" @ok="modalFormOk"/>
    <GoodsShelfModalEdit ref="modalFormEdit" @ok="modalFormOk"/>


    <!--审核弹框-->
    <a-modal v-model="hovered" title="审核" on-ok="handleOk" :footer="null" :width="300" :centered="true">
      <div style="display: flex;flex-direction: row;justify-content: center">
        <a-button key="拒绝" type="danger" :loading="loadingid" @click="shenhe(shenheId,3)" style="margin-right: 15px">
          拒绝
        </a-button>
        <a-button key="通过" type="primary" :loading="loadingid" @click="shenhe(shenheId,2)" >
          通过
        </a-button>
      </div>


    </a-modal>


  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import GoodsShelfModal from './modules/GoodsShelfModal'
  import GoodsShelfModalEdit from './modules/GoodsShelfModalEdit'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import '@/assets/less/TableExpand.less'
  import {postAction,getAction} from '../../api/manage'
  import pick from 'lodash.pick'
  import JPopup from '@/components/jeecg/JPopup'

  export default {
    name: "GoodsShelfList",
    mixins:[JeecgListMixin],
    components: {
      GoodsShelfModal,
      JPopup,
      GoodsShelfModalEdit
    },
    data () {
      return {
        form: this.$form.createForm(this),
        loadingid:false,
        shenheId:'',
        goodsName:'',
        hovered:false,
        description: '商品上架管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'商品',
            align:"center",
            dataIndex: 'goodsName'
          },
          {
            title:'标题',
            align:"center",
            dataIndex: 'title'
          },
          {
            title:'开始时间',
            align:"center",
            dataIndex: 'startTime'
          },
          {
            title:'结束时间',
            align:"center",
            dataIndex: 'endTime'
          },
          {
            title:'商品状态',
            align:"center",
            dataIndex: 'status_dictText',
            scopedSlots: { customRender: 'status' },
          },
          {
            title:'审核状态',
            align:"center",
            dataIndex: 'applyStatus_dictText',
            scopedSlots: { customRender: 'applyStatus_dictText' },
          },
          {
            title:'新品状态',
            align:"center",
            dataIndex: 'newStatus_dictText'
          },
          {
            title:'推荐状态',
            align:"center",
            dataIndex: 'recommandStatus_dictText'
          },
          {
            title:'排序',
            align:"center",
            dataIndex: 'sort'
          },
          {
            title:'佣金',
            align:"center",
            dataIndex: 'commission'
          },
          {
            title:'积分',
            align:"center",
            dataIndex: 'integral'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/goods/goodsShelf/list",
          delete: "/goods/goodsShelf/delete",
          deleteBatch: "/goods/goodsShelf/deleteBatch",
          exportXlsUrl: "/goods/goodsShelf/exportXls",
          importExcelUrl: "goods/goodsShelf/importExcel",
          goodsUpDown:"goods/goodsShelf/goodsUpDown",
          queryById:"goods/goodsShelf/queryById?id=",
          send:"goods/goodsShelf/send/apply/",
          shenhe:"goods/goodsShelf/apply/"
        },
        dictOptions:{},
      }
    },
    created() {
      /*let fieldval = pick('goodsId','goodsName')
      this.$nextTick(() => {
        this.form.setFieldsValue(fieldval)
      })*/
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      //查询请求参数 所有的报表组件都可以用这个方法显示中文情况下 查ID tigger和callback是关键
      popupCallback(e){
        // console.log(e)
        this.queryParam.goodsId = e.goodsId
      },
      shenheid(id){
        this.shenheId = id
        this.hovered = true
      },
      handleClickChange(visible){
        // this.clicked = true
        // this.clicked = false;
        this.hovered = visible;
      },
      /*审核*/
      shenhe(id,state){
        this.loading = true
        this.loadingid = true
        getAction(this.url.shenhe+id+'/'+state).then((res)=>{
          if (res.success){

            setTimeout(()=>{
              this.$message.success(res.message)
              this.loading = false
              this.loadingid = false
              this.hovered = false
              this.loadData()
            },2000)

          }else {
            this.hovered = false

            this.loading = false
            this.loadingid = false

            this.$message.error(res.message)
          }
        })
      },
      /*送审*/
      handleSend(id){
        getAction(this.url.send+id).then((res)=>{
          console.log(res)
          if (res.success){
            this.$message.success(res.message)
            this.loadData()
          }
        })
      },
      initDictConfig(){
      },
      //上架===下架   上下架之前先提交状态和ID 判断是否在后台已经自动上下架了  返回相应提示和操作
      handleGoodsUpDown(record){
        this.loading = true
        postAction(this.url.goodsUpDown,{id:record.id,status:record.status}).then((res)=>{
            console.log(res)
          if (res.success){
            this.loading = false
            this.loadData()
          }else {
            this.$message.error(res.message)
            this.loading = false
            this.loadData()
          }
        })
      },

      //编辑======先查询该条商品ID是否自动上架了 ，判断状态  如果上架了 就提示一下 刷新列表  编辑消失   如果不是  覆盖record 打开编辑窗口
      handleEditCustom: function (record) {
        this.loading = true
        getAction(this.url.queryById+record.id).then((res)=>{
          console.log(res)

          if (res.result.status==0){
            this.loading = false

            this.$refs.modalForm.title = "编辑";
            this.$refs.modalForm.disableSubmit = false;
            // this.$refs.modalForm.disableFixNum = true;
            this.$refs.modalForm.edit(res.result);
          }else {
            this.$message.error('该商品已上架，不可编辑')
            this.loading = false
            this.loadData()
          }
        })




      },

      handleDetail:function(record){

        this.$refs.modalForm.title="详情";
        this.$refs.modalForm.disableSubmit = true;
        // this.$refs.modalForm.disableFixNum = false;
        this.$refs.modalForm.edit(record);
        // console.log('disableFixNum',this.$refs.modalForm.disableFixNum)
      },
      /*修改数量*/
      handleFixNum:function(record){

        this.$refs.modalFormEdit.title="修改数量";
          // this.$refs.modalFormEdit.disableSubmit = true;
          // this.$refs.modalFormEdit.disableFixNum = true;
        this.$refs.modalFormEdit.edit(record);

      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
  .ant-switch-disabled{
    cursor: pointer;
    opacity: 1;
  }
  .ssss{
    cursor: pointer !important;
  }
  .ant-switch{
    background-color: rgb(255, 73, 73);
  }
  .ant-switch-checked{
    background-color: #1890FF !important;
  }
  .ant-btn-danger{
    background: #ff4d4f !important;
  }
</style>